﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Fated {
    public class InputState {
        private GamePadState oldPadState;
        private KeyboardState oldKeyState;
        private MouseState oldMouseState;

        private GamePadState newPadState;
        private KeyboardState newKeyState;
        private MouseState newMouseState;

        public InputState(GamePadState pad) {
            oldPadState = pad;
            newPadState = pad;
            SelectButton = Buttons.A;
            CancelButton = Buttons.B;
            MenuButton = Buttons.Start;
        }
        public InputState(KeyboardState pad) {
            oldKeyState = pad;
            newKeyState = pad;
            SelectKey = Keys.Enter;
            CancelKey = Keys.Space;
            MenuKey = Keys.RightShift;

            PrimaryUpKey = Keys.Up;
            PrimaryDownKey = Keys.Down;
            PrimaryRightKey = Keys.Right;
            PrimaryLeftKey = Keys.Left;

            SecondaryUpKey = Keys.W;
            SecondaryDownKey = Keys.S;
            SecondaryRightKey = Keys.D;
            SecondaryLeftKey = Keys.A;
        }
        public InputState(MouseState pad) {
            oldMouseState = pad;
            newMouseState = pad;
        }

        public void updateState(GamePadState newPad) {
            oldPadState = newPadState;
            newPadState = newPad;
        }
        public void updateState(KeyboardState newPad) {
            oldKeyState = newKeyState;
            newKeyState = newPad;
        }
        public void updateState(MouseState newPad) {
            oldMouseState = newMouseState;
            newMouseState = newPad;
        }

        public bool SelectButtonPress {
            get {
                return newPadState.IsButtonDown(SelectButton) ||
                    newKeyState.IsKeyDown(SelectKey) ||
                    newMouseState.LeftButton == ButtonState.Pressed;
            }
        }
        public bool CancelButtonPress {
            get {
                return newPadState.IsButtonDown(CancelButton) ||
                    newKeyState.IsKeyDown(CancelKey) ||
                    newMouseState.RightButton == ButtonState.Pressed;
            }
        }
        public bool MenuButtonPress {
            get {
                return newPadState.IsButtonDown(MenuButton) ||
                    newKeyState.IsKeyDown(MenuKey);
            }
        }

        public bool UniqueSelectButtonPress {
            get {
                return (newPadState.IsButtonDown(SelectButton) && oldPadState.IsButtonUp(SelectButton)) ||
                    (newKeyState.IsKeyDown(SelectKey) && oldKeyState.IsKeyUp(SelectKey)) ||
                    (newMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released);
            }
        }
        public bool UniqueCancelButtonPress {
            get {
                return (newPadState.IsButtonDown(CancelButton)  && oldPadState.IsButtonUp(CancelButton))||
                    (newKeyState.IsKeyDown(CancelKey) && oldKeyState.IsKeyUp(CancelKey)) ||
                    (newMouseState.RightButton == ButtonState.Pressed && oldMouseState.RightButton == ButtonState.Released);
            }
        }
        public bool UniqueMenuButtonPress {
            get {
                return (newPadState.IsButtonDown(MenuButton) && oldPadState.IsButtonUp(MenuButton)) ||
                    (newKeyState.IsKeyDown(MenuKey) && oldKeyState.IsKeyUp(MenuKey));
            }
        }

        public Vector2 PrimaryStick {
            get {
                return newPadState.ThumbSticks.Left +
                    new Vector2((newKeyState.IsKeyDown(PrimaryLeftKey) ? -1 : 0) + (newKeyState.IsKeyDown(PrimaryRightKey)  ? 1 : 0),
                        (newKeyState.IsKeyDown(PrimaryUpKey) ? 1 : 0) + (newKeyState.IsKeyDown(PrimaryDownKey)  ? -1 : 0));
            }
        }
        public Vector2 SecondaryStick {
            get {
                return newPadState.ThumbSticks.Right +
                    new Vector2((newKeyState.IsKeyDown(SecondaryLeftKey) ? -1 : 0) + (newKeyState.IsKeyDown(SecondaryRightKey) ? 1 : 0),
                        (newKeyState.IsKeyDown(SecondaryUpKey) ? 1 : 0) + (newKeyState.IsKeyDown(SecondaryDownKey) ? -1 : 0));
            }
        }
        public Vector2 DPad {
            get {
                return new Vector2((newPadState.DPad.Left == ButtonState.Pressed ? -1 : 0) + (newPadState.DPad.Right == ButtonState.Pressed ? 1 : 0),
                        (newPadState.DPad.Up == ButtonState.Pressed ? 1 : 0) + (newPadState.DPad.Down == ButtonState.Pressed ? -1 : 0));
            }
        }

        public bool NewLeftBumperPress {
            get {
                return (newPadState.Buttons.LeftShoulder == ButtonState.Pressed && oldPadState.Buttons.LeftShoulder == ButtonState.Released) ||
                    (newKeyState.IsKeyDown(LeftShoulderKey) && oldKeyState.IsKeyUp(LeftShoulderKey));
            }
        }
        public bool NewRightBumperPress {
            get {
                return (newPadState.Buttons.RightShoulder == ButtonState.Pressed && oldPadState.Buttons.RightShoulder == ButtonState.Released) ||
                    (newKeyState.IsKeyDown(RightShoulderKey) && oldKeyState.IsKeyUp(RightShoulderKey));
            }
        }

        public Buttons SelectButton { get; set; }
        public Buttons CancelButton { get; set; }
        public Buttons MenuButton { get; set; }

        public Keys SelectKey { get; set; }
        public Keys CancelKey { get; set; }
        public Keys MenuKey { get; set; }

        public Keys PrimaryUpKey { get; set; }
        public Keys PrimaryDownKey { get; set; }
        public Keys PrimaryLeftKey { get; set; }
        public Keys PrimaryRightKey { get; set; }

        public Keys SecondaryUpKey { get; set; }
        public Keys SecondaryDownKey { get; set; }
        public Keys SecondaryLeftKey { get; set; }
        public Keys SecondaryRightKey { get; set; }

        public Keys LeftShoulderKey { get; set; }
        public Keys RightShoulderKey { get; set; }
    }
}
